home *** CD-ROM | disk | FTP | other *** search
- Path: dekalb.dc.peachnet.edu!not-for-mail
- From: williamh@dekalb.dc.peachnet.edu (darlene williams)
- Newsgroups: comp.lang.c++
- Subject: Re: Can copy constructor and operator= share code?
- Date: 5 Mar 1996 19:56:48 -0500
- Organization: DeKalb College
- Sender: gregory ulyssis narizny
- Message-ID: <4hinsg$ooq@dekalb.dc.peachnet.edu>
- References: <4h2kcn$40d@rap.SanDiegoCA.ATTGIS.COM> <VA.00000053.00cdab05@fred>
- NNTP-Posting-Host: dekalb.dc.peachnet.edu
-
- i noticed that there is a lot of confusion lately in this group about the
- difference between assignment operator, default, and copy constructors:
-
- >(Boris Burtin) wrote:
- >> I have noticed that a copy constructor and operator= perform pretty
- >> much the same function..
-
- yes, but the context of the operation is different:
- copy c-tor: newly created instance
- = op.: copies after the instance has been instantiated
- the main difference in terms of your code is that any post-construction
- initialization may have to be undone, i.e., de-allocation of resources
- done at construction.
-
- >> Is there a way for the one of the two functions to call the other, to
- >> avoid duplicate code?
-
- i have adopted using a "copy()" member function; the benefit is a single
- piece of code, and the cost is the probable double-initialization of
- class members:
-
- class foo
- {
- public:
- foo () { }
- foo &operator = (const foo &rhs) { copy (rhs); return (*this); }
- foo (const foo &rhs) { copy (rhs); }
- void copy (const foo &rhs) { b = rhs.b; }
- private:
- bar b;
- };
-
- the cost referred to above is that 'bar b' will get initialized twice
- in the copy constructor case, once by its default c-tor, and again by
- its assignment operator ("b=rhs.b"), as opposed to:
-
- void foo::foo (const foo &rhs) b(rhs.b) { }
-
- as to this approach:
-
- >T &T::operator =(const T &t) { ~T(); new(this) T(t); }
-
- this is a somewhat interesting approach, but destruction isn't
- always the same as "resetting" the object to a pre-copy state.
- i prefer including a "reset()" function whose semantics imply
- 'reset to at-construction state', and not have to worry about
- any side-effects of destroying the object:
- T &T::operator = (const T &t) { reset(); copy(t); return(*this); }
-
-
- on another note: i haven't visited this group (or the net) in a while..
- are there any groups like "comp.lang.c++.wizards"?; i'm looking for
- info beyond c++ mechanics:
-
- -- has anyone had experiences/comment re. coplien's exemplars
- (class community bulletin board) technique?
- -- a year ago, i saw a reference to a book that went into
- c++ mixins.. where can i find more mixin info?
- (john max skaller.. you still out there??)
- -- what's the latest on RTTI?
-
-
- gregory narizny
-
- greg@bossy.bst.bls.com
-
- -- currently doing multi-point video conference scheduling systems
- (in the middle of the phone deregulation wars!)
- -- commercial inquiries for s/w development work always welcome
-
-